package com.changshengwei;

import java.awt.*;
import java.nio.file.DirectoryStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.*;

/**
 * @author 07509-Chang Shengwei
 * @version Id: LambdaTest.java, v 0.1 2021/6/3 15:14 Chang Shengwei Exp $
 * @description
 */
public class LambdaTest {

    public static void main(String[] args) {

        Comparator<Apple> comparator = new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getColor().compareTo(o2.getColor());
            }
        };

        List<Apple> list = Collections.emptyList();

        list.sort(comparator);

        Comparator<Apple> comparator2 = (o1, o2) -> o1.getColor().compareTo(o2.getColor());

        list.sort(comparator2);

        Comparator<Apple> comparator3 = Comparator.comparing(Apple::getColor);

        list.sort(comparator3);

        /**
         * valid lambda
         */

        // function lambda
        Function<String, Integer> functionLambda = s -> s.length();

        // predicate lambda
        Predicate<Apple> predicateLambda = a -> "green".equals(a.getColor());

        // supplier lambda
       Supplier<Apple> aNew = Apple::new;

       Supplier<Apple> bNew = new Supplier<Apple>() {
           @Override
           public Apple get() {
               return new Apple();
           }
       };

       // consumer lambda
        Consumer<Apple> consumer = a -> System.out.println(a.getColor());
    }
}
